{
	"translatorID":"1aa25ce9-ac43-42c1-b096-14648bd1823c",
	"translatorType":4,
	"label":"cnpat",
	"creator":"Huang Wei<i@huangwei.me>",
	"target":"^http://search.cnpat.com.cn",
	"minVersion":"1.0",
	"maxVersion":"",
	"priority":100,
	"inRepository":false,
	"lastUpdated":"11:21 2010-02-07"
}

function detectWeb(doc, url) {
	
	if (doc.location.href.match("ViewSearch")) {
		return "patent";
	} else if (doc.location.href.match("tableSearch")) {
		return "multiple";
	}
	
}

function scrape(doc, url) {
    var namespace = doc.documentElement.namespaceURI;
    var nsResolver = namespace ? function(prefix) {
        if (prefix == "x") return namespace; else return null;
    } : null;


    //获取的Zotero对象类型为专利
    var newItem = new Zotero.Item("patent");

    if (doc.location.href.match("ViewSearch")) {
        // 附件，保存当前页的网页快照
        newItem.attachments.push({document:doc, title:"web snapshot"});

        //专利名称
        var title = doc.evaluate('//table/tbody/tr[9]/td[2]', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().textContent;
        Zotero.debug("专利名称:"+title);
        newItem.title = title;
        //摘要
        var abstract = doc.evaluate('//table/tbody/tr[17]/td', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().textContent;
        Zotero.debug("摘要:"+abstract);
        newItem.abstractNote = abstract;
        //申请号/专利号
        var appNo = doc.evaluate('//table/tbody/tr[3]/td[2]', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().textContent;
        Zotero.debug("申请号/专利号:"+appNo);
        newItem.patentNumber = appNo;
        newItem.publicationNumber = appNo;
        //公开日
        var pubDate = doc.evaluate('//table/tbody/tr[4]/td[2]', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().textContent;
        Zotero.debug("公开日:"+pubDate);
        newItem.issueDate = pubDate;
        //申请日
        var appDate = doc.evaluate('//table/tbody/tr[3]/td[4]', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().textContent;
        Zotero.debug("申请日:"+appDate);
	newItem.date = appDate;

        //授权公告日
        var authPubDate = doc.evaluate('//table/tbody/tr[6]/td[4]', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().textContent;
        if(authPubDate)
        {
            Zotero.debug("授权公告日:"+authPubDate);
            newItem.issueDate = authPubDate;
        }
        //申请人/专利权人 
        var appPersons = doc.evaluate('/html/body/table/tbody/tr[13]/td[2]', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().textContent;
        Zotero.debug("申请人/专利权人:"+appPersons);
        newItem.assignee = appPersons;
        //申请人地址
        var appAddr = doc.evaluate('/html/body/table/tbody/tr[14]/td[2]', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().textContent;
        Zotero.debug("申请人地址:"+appAddr);
        newItem.place= appAddr;
        //发明设计人
        var inventor = doc.evaluate('/html/body/table/tbody/tr[12]/td[2]', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().textContent;
        Zotero.debug("发明设计人:"+inventor);
        var invertors = inventor.split(";");
        for(var m = 0;m < invertors.length;m++)
        {
            var author = invertors[m];
            newItem.creators.push(Zotero.Utilities.cleanAuthor(author, "inventor"));
        }

        //分类号
        var classificationNo = doc.evaluate('/html/body/table/tbody/tr[10]/td[2]', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().textContent;
        Zotero.debug("分类号:"+classificationNo);
        newItem.extra = classificationNo;

        //优先权项
        var prioNum = doc.evaluate('/html/body/table/tbody/tr[21]/td', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().textContent;
        Zotero.debug("优先权项:"+prioNum);
        newItem.priorityNumbers = prioNum;
        //主权利要求
        var rights = doc.evaluate('/html/body/table/tbody/tr[19]/td', doc, nsResolver, XPathResult.ANY_TYPE, null).iterateNext().textContent;
        Zotero.debug("主权利要求:"+rights);
        newItem.rights = rights;
        //访问URL
        newItem.url = doc.location.href;

    }

    Zotero.debug("finished.");
    newItem.complete();
}

function doWeb(doc, url) {
    var namespace = doc.documentElement.namespaceURI;
    var nsResolver = null;
    var items = new Object();
    var urls = new Array();

    if (detectWeb(doc, url) == "multiple") {
        var myXPath = '//table/tbody/tr/td[3]/a';
        var titles = doc.evaluate(myXPath, doc, nsResolver, XPathResult.ANY_TYPE, null);
        while (nextTitle = titles.iterateNext()) {
            items[nextTitle.href] = nextTitle.textContent;
            Zotero.debug(items[nextTitle.href]);
        }
        items = Zotero.selectItems(items);
        for (var i in items) {
            urls.push(i);
        }
    } else {
        urls = [url];
    }

    Zotero.debug(urls);

    Zotero.Utilities.processDocuments(urls, scrape, function() { Zotero.done(); });
    Zotero.wait();
}

